package com.x.ant.dao;

import com.x.ant.model.Truck;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class TruckDao extends BaseDao {

    private final static String selectTruckListSql = "SELECT T1.*, T3.USER_MOBILE, T3.USER_NAME, T3.USER_SEX, T3.USER_NO " +
            "FROM ANT_TRUCK T1, ANT_DEPT_USER T2, ANT_USER T3 " +
            "WHERE T3.USER_ID = T2.USER_ID AND T3.USER_ID = T1.USER_ID ";
    public List<Truck> selectTruckList(Truck truck, String deptParentId) throws Exception{
        StringBuilder sb = new StringBuilder(selectTruckListSql);
        List params = new ArrayList();
        if (this.isString(deptParentId)) {
            sb.append("AND T2.DEPT_PARENT_ID = ? ");
            params.add(deptParentId);
        }
        if (this.isString(truck.getTruckId())) {
            sb.append("AND T1.TRUCK_ID = ? ");
            params.add(truck.getTruckId());
        }
        if (this.isString(truck.getTruckMobile())) {
            sb.append("AND T1.TRUCK_MOBILE = ? ");
            params.add(truck.getTruckMobile());
        }
        if (this.isString(truck.getTruckPlateHeader())) {
            sb.append("AND T1.TRUCK_PLATE_HEADER = ? ");
            params.add(truck.getTruckPlateHeader());
        }
        if (this.isString(truck.getTruckPlateMiddle())) {
            sb.append("AND T1.TRUCK_PLATE_MIDDLE = ? ");
            params.add(truck.getTruckPlateMiddle());
        }
        if (this.isString(truck.getTruckPlateCode())) {
            sb.append("AND T1.TRUCK_PLATE_CODE = ? ");
            params.add(truck.getTruckPlateCode());
        }
        return this.queryForList(sb.toString(), Truck.class, params, truck);
    }

    private final static String selectTruckNumSql = "SELECT COUNT(1) " +
            "FROM ANT_TRUCK T1, ANT_DEPT_USER T2, ANT_USER T3 " +
            "WHERE T3.USER_ID = T2.USER_ID AND T1.USER_ID = T3.USER_ID ";
    public int selectTruckNum(Truck truck, String deptParentId) throws Exception{
        StringBuilder sb = new StringBuilder(selectTruckNumSql);
        List params = new ArrayList();
        if (this.isString(deptParentId)) {
            sb.append("AND T2.DEPT_PARENT_ID = ?");
            params.add(deptParentId);
        }
        if (this.isString(truck.getTruckId())) {
            sb.append("AND T1.TRUCK_ID = ? ");
            params.add(truck.getTruckId());
        }
        if (this.isString(truck.getTruckMobile())) {
            sb.append("AND T1.TRUCK_MOBILE = ? ");
            params.add(truck.getTruckMobile());
        }
        if (this.isString(truck.getTruckPlateHeader())) {
            sb.append("AND T1.TRUCK_PLATE_HEADER = ? ");
            params.add(truck.getTruckPlateHeader());
        }
        if (this.isString(truck.getTruckPlateMiddle())) {
            sb.append("AND T1.TRUCK_PLATE_MIDDLE = ? ");
            params.add(truck.getTruckPlateMiddle());
        }
        if (this.isString(truck.getTruckPlateCode())) {
            sb.append("AND T1.TRUCK_PLATE_CODE = ? ");
            params.add(truck.getTruckPlateCode());
        }
        if(truck.getTruckCreateTime() != null){
            sb.append("AND DATE(T1.TRUCK_CREATE_TIME) = ? ");
            params.add(truck.getTruckCreateTime());
        }
        return this.queryForInt(sb.toString(), params);
    }

}
